package cn.zhou.service.impl;

import cn.zhou.service.IMessageProvider;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @Description:
 * @Author: zhou
 * @Date: Create in 17:05 2020/7/31
 */
//@Service：这里不需要了，这里不是传统的controller调用service。这个service是和rabbitMQ打交道的
@EnableBinding(Source.class) //可以理解为我们定义一个消息生产者的发送管道
public class IMessageProviderImpl implements IMessageProvider {
    @Resource
    private MessageChannel output;  //消息放送管道

    @Override
    public String send() {

        String serial = UUID.randomUUID().toString();

        //通过output这个管道向消息中间件发送消息
        Message<String> message = MessageBuilder.withPayload(serial).build();
        output.send(message);

        //output.send(MessageBuilder.withPayload(serial).build());

        System.out.println("*********serial: " + serial);

        return null;
    }

}
